|
1
|
|
|
'use strict'; |
|
2
|
|
|
|
|
3
|
|
|
var os = require('os'), |
|
4
|
|
|
EOL = os.EOL, |
|
5
|
|
|
Config = require('./config'), |
|
6
|
|
|
Position = require('./position'), |
|
7
|
|
|
Grid = require('./grid'), |
|
8
|
|
|
Mower = require('./mower'); |
|
9
|
|
|
|
|
10
|
|
|
// Expose `App`. |
|
11
|
|
|
|
|
12
|
|
|
module.exports = App; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Set up App with `options` |
|
17
|
|
|
* which is responsible for multiple-unit mower's remote control |
|
18
|
|
|
* |
|
19
|
|
|
* Options: |
|
20
|
|
|
* |
|
21
|
|
|
* - `configFile` config file path |
|
22
|
|
|
* |
|
23
|
|
|
* @param {Object} options |
|
24
|
|
|
* @api public |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
function App(options) { |
|
28
|
|
|
this.options = options || {}; |
|
29
|
|
|
this.config = new Config(this.options.configFile); |
|
30
|
|
|
this.logEnabled = this.options.debug || false; |
|
31
|
|
|
this.grid = new Grid(this.config.topRightPos); |
|
32
|
|
|
this.mowers = this.createMowers(this.config.mowers); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Start mower's movement. |
|
37
|
|
|
* |
|
38
|
|
|
* @api protected |
|
39
|
|
|
*/ |
|
40
|
|
|
|
|
41
|
|
|
App.prototype.run = function() { |
|
42
|
|
|
var self = this; |
|
43
|
|
|
this.mowers.forEach(function(mower) { |
|
44
|
|
|
mower.start().then(function(mower) { |
|
45
|
|
|
self.log('Mower '+mower.id+' stopped at position: x:' + |
|
46
|
|
|
mower.position.x+' y:'+mower.position.y+' cardinal: '+mower.position.c+EOL) |
|
47
|
|
|
}); |
|
48
|
|
|
}); |
|
49
|
|
|
}; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Create mowers |
|
53
|
|
|
* |
|
54
|
|
|
* @param {Array} mowersConf |
|
55
|
|
|
* @api protected |
|
56
|
|
|
*/ |
|
57
|
|
|
|
|
58
|
|
|
App.prototype.createMowers = function(mowersConf) { |
|
59
|
|
|
var mowers = []; |
|
60
|
|
|
for(var i=0; i<mowersConf.length; i++) { |
|
61
|
|
|
var position = new Position(mowersConf[i].pos, mowersConf[i].cardinal); |
|
62
|
|
|
mowers.push(new Mower(i, this.grid, position, mowersConf[i].instructions)); |
|
63
|
|
|
} |
|
64
|
|
|
return mowers; |
|
65
|
|
|
}; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Stdout filtered by log option |
|
69
|
|
|
* |
|
70
|
|
|
* @param {String} message |
|
71
|
|
|
* @api protected |
|
72
|
|
|
*/ |
|
73
|
|
|
|
|
74
|
|
|
App.prototype.log = function(message) { |
|
75
|
|
|
if(this.logEnabled) { |
|
76
|
|
|
process.stdout.write(message); |
|
77
|
|
|
} |
|
78
|
|
|
}; |
|
79
|
|
|
|